package view.manage.reports;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.table.DefaultTableModel;
import model.Report;
import main.WinkelApplication;
import java.util.List;
import javax.swing.JOptionPane;
import model.Category;
/**
 *
 * @author JJ Zeilmaker
 * df
 */
public class view extends javax.swing.JPanel {

    private String fCategory = null;
    private String fDateFrom = null;    
    private String fDateTill = null;    
    
    /** Creates new form view */
    public view() {
        initComponents();
        setProductList();
        setStandartInput();
        setCBCategories();
    } 
 
    /*
     * Get all Category`s from database and addItem to cbCatogorys combobox for filter selection
     * 
     */
    void setCBCategories() {       
        List<Category> categories = WinkelApplication.getQueryManager().getCategories();
        for(Category cat : categories) {
            if(!cat.getName().equals("")) { cbCatogorys.addItem(cat.getName()); }
        }
    }    
    
    /*
     * Using Calendar to get and set the current date and current date -1 month to 
     * input value`s for the standart filter config.
     * txtDateFrom =  current date -1 month
     * txtDateTill =  current date
     * ComboSelect =  set to 0 default
     */
    void setStandartInput(){        
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        String curDate = dateformat.format(cal.getTime());
        cal.add(Calendar.MONTH, -1);	//Adding 1 month to current date
        String newDate = dateformat.format(cal.getTime());
        this.fDateFrom = newDate;
        this.fDateTill = curDate;
        txtDateFrom.setText(newDate);
        txtDateTill.setText(curDate);    
        this.cbCatogorys.setSelectedIndex(0);
        setProductList();
    }

   public void cleanTable( DefaultTableModel model ) {
        for( int i = model.getRowCount() - 1; i >= 0; i-- )
        {
            model.removeRow(i);
        }
    }
    
    /*
     * Get all Products and set them into table
     * Check what filters are use add to sql query
     */   
    public void setProductList(){
        String sqlString = ""; 
        fCategory = this.cbCatogorys.getSelectedItem().toString();
        if(fCategory != "Alles"){ sqlString = "categorie.naam = '"+fCategory+"'"; }
        String dateFrom = txtDateFrom.getText();
        String dateTill = txtDateTill.getText();
        
        if(dateFrom != "" && dateTill != ""){
            if(sqlString.equals("")){ sqlString = "WHERE "; } else { sqlString = " WHERE "+sqlString+" AND "; }
            sqlString = sqlString+"datum >= '"+dateFrom+"' AND datum <= '"+dateTill+"'";
        }

        
        List<Report> orders = WinkelApplication.getQueryManager().getOrderList(sqlString);
        DefaultTableModel model = (DefaultTableModel) this.jTable1.getModel();
        this.cleanTable(model);
        int total = 0;
        for(Report order: orders){     
            model.addRow(
                    new Object[] {
                        order.getProduct_name(),
                        order.getCategory_name(),
                        order.getAantal(),
                        order.getTprijs()
                           }
                    );
            total += order.getAantal();         
        }           
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        lblDateFrom = new javax.swing.JLabel();
        txtDateFrom = new javax.swing.JTextField();
        lblDateTill = new javax.swing.JLabel();
        txtDateTill = new javax.swing.JTextField();
        cbCatogorys = new javax.swing.JComboBox();
        btnFilter = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnReset = new javax.swing.JButton();

        jButton1.setText("jButton1");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Product", "Categorie", "Aantal", "Totaal €"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);

        lblDateFrom.setText("Van");

        txtDateFrom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDateFromActionPerformed(evt);
            }
        });

        lblDateTill.setText("Tot");

        txtDateTill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDateTillActionPerformed(evt);
            }
        });

        cbCatogorys.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Alles" }));
        cbCatogorys.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbCatogorysActionPerformed(evt);
            }
        });

        btnFilter.setText("Filter");
        btnFilter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFilterActionPerformed(evt);
            }
        });

        btnBack.setText("Terug");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        jLabel1.setText("Categorie");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel2.setText("Rapporten");

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(lblDateTill)
                    .addComponent(lblDateFrom)
                    .addComponent(cbCatogorys, 0, 128, Short.MAX_VALUE)
                    .addComponent(txtDateTill, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                    .addComponent(txtDateFrom, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnFilter))
                    .addComponent(jLabel1)
                    .addComponent(btnBack))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 583, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(lblDateFrom)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDateFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblDateTill)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDateTill, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbCatogorys, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnFilter)
                            .addComponent(btnReset))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 213, Short.MAX_VALUE)
                        .addComponent(btnBack)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtDateFromActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDateFromActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDateFromActionPerformed

    private void txtDateTillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDateTillActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDateTillActionPerformed

    private void cbCatogorysActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbCatogorysActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbCatogorysActionPerformed

    private void btnFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFilterActionPerformed
        //this fCategory = null;
        fDateFrom = this.txtDateFrom.getText();    
        fDateTill = this.txtDateTill.getText();
        fCategory = this.cbCatogorys.getSelectedItem().toString();
        //if(this.searchCheckbox.isSelected() == true) {
        if(fDateFrom.equals("") || fDateTill.equals("")){
           JOptionPane.showMessageDialog(WinkelApplication.getInstance().getMainWindow(), "Datum van en Datum tot moeten worden ingevult yyyy-mm-dd", "Error", JOptionPane.ERROR_MESSAGE);
        }else{
         this.setProductList();       
        }       

    }//GEN-LAST:event_btnFilterActionPerformed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        WinkelApplication.window.previous();
    }//GEN-LAST:event_btnBackActionPerformed
    /*
     * Reset Button
     * This will call the function setStandartInput() and will reset the filter input`s
     */
    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
       this.setStandartInput();
    }//GEN-LAST:event_btnResetActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnFilter;
    private javax.swing.JButton btnReset;
    private javax.swing.JComboBox cbCatogorys;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblDateFrom;
    private javax.swing.JLabel lblDateTill;
    private javax.swing.JTextField txtDateFrom;
    private javax.swing.JTextField txtDateTill;
    // End of variables declaration//GEN-END:variables

      
}
